#include "dialogs.h"
#include "ui_messagedlg.h"
#include "ui_waitdlg.h"
#include "ui_SplashScreen.h"
#include <qmovie.h>




SplashScreen::SplashScreen(QString projectName, QWidget *parent): QSplashScreen(parent), ui(new Ui::SplashScreen) {
     ui->setupUi(this);
     ui->progressBar->setValue(0);
     ui->projectNameLabel->setText(projectName);
     int x = (this->screen()->size().width() - this->size().width()) / 2;
     int y = (this->screen()->size().height() - this->size().height()) / 2;
     this->move(x, y);
}

void SplashScreen::update(QString msg, int progress) {
     ui->msgLabel->setText(msg);
     if (progress > 0) {
         ui->progressBar->setValue(progress);
     }
}

MessageDlg::MessageDlg(QWidget *parent) : QDialog(parent), ui(new Ui::MessageDlg)
{
    ui->setupUi(this);
    if (parent != nullptr) {
        this->setWindowTitle(parent->windowTitle());
    }
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setWindowModality(Qt::NonModal);
}

MessageDlg::MessageDlg(QPixmap& icon, QWidget *parent, QString title, QString text, QString acceptBtn, QString rejectBtn):MessageDlg(parent)
{
    ui->logoLabel->setPixmap(icon);
    ui->titleLabel->setText(title);
    ui->msgLabel->setText(text);
    if (!acceptBtn.isEmpty()) {
        ui->acceptBtn->setText(acceptBtn);
    }
    else {
        ui->acceptBtn->hide();
    }
    if (!rejectBtn.isEmpty()) {
        ui->rejectBtn->setText(rejectBtn);
    }
    else {
        ui->rejectBtn->hide();
    }
}

MessageDlg::~MessageDlg()
{
    delete ui;
}

WaitDlg::WaitDlg(QString msg, QWidget *parent) : QDialog(parent), ui(new Ui::WaitDlg) {
    ui->setupUi(this);
    this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    this->setWindowOpacity(1);
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setModal(true);
    //setWindowModality(Qt::NonModal);
    QMovie* movie = new QMovie(QString::fromUtf8(":/images/wait.gif"));
    ui->label->setMovie(movie);
    ui->titleLabel->setText(msg);
    movie->start();
    this->show();
}

WaitDlg::~WaitDlg() {
    delete ui;
}

void WaitDlg::closeDlg()
{
    this->close();
    this->deleteLater();
}

MyMessageBox::MyMessageBox() {}

int MyMessageBox::information(QWidget *parent, QString title, QString text, QString acceptBtn, QString rejectBtn) {
//    QPixmap icon = QPixmap(QString::fromUtf8(":/images/info.svg"));
    QPixmap icon(QString::fromUtf8(":/images/info.svg"));
    MessageDlg box(icon, parent, title, text, acceptBtn, rejectBtn);
    return box.exec();
}

int MyMessageBox::warning(QWidget *parent, QString title, QString text, QString acceptBtn, QString rejectBtn) {
//    QPixmap icon = QPixmap(QString::fromUtf8(":/images/warning.svg"));
    QPixmap icon(QString::fromUtf8(":/images/warning.svg"));
    MessageDlg box(icon, parent, title, text, acceptBtn, rejectBtn);
    return box.exec();
}

int MyMessageBox::error(QWidget *parent, QString title, QString text, QString acceptBtn, QString rejectBtn) {
//    QPixmap icon = QPixmap(QString::fromUtf8(":/images/error.svg"));
    QPixmap icon(QString::fromUtf8(":/images/error.svg"));
    MessageDlg box(icon, parent, title, text, acceptBtn, rejectBtn);
    return box.exec();
}

int MyMessageBox::question(QWidget *parent, QString title, QString text, QString acceptBtn, QString rejectBtn) {
//    QPixmap icon = QPixmap(QString::fromUtf8(":/images/question.svg"));
    QPixmap icon(QString::fromUtf8(":/images/question.svg"));
    MessageDlg box(icon, parent, title, text, acceptBtn, rejectBtn);
    return box.exec();
}

WaitDlg *MyMessageBox::showWait(QString msg, QWidget* parent) {
    return new WaitDlg(msg, parent);
}

void MyMessageBox::closeWaitDlg(WaitDlg *&dlg)
{
    if(dlg != nullptr){
        dlg->close();
        dlg->deleteLater();
        dlg = NULL;
    }
}
